/*
 * 版权所有(C)，上海海鼎信息工程股份有限公司，lizhixing，所有权利保留。
 * <p>
 * 项目名：	multi-thread
 * 文件名：	DeadLockDemo
 * 模块说明：
 * 修改历史：
 * 2022/8/1 - lizhixing - 创建。
 */
package com.lovely602.thread.lock;

/**
 * @author lizhixing
 */
public class DeadLockDemo {

    private static String A = "A";

    private static String B = "B";

    public static void main(String[] args) {
        new DeadLockDemo().deadLock();
    }

    private void deadLock() {

        Thread t1 = new Thread(() -> {
            System.out.println("线程t1等待获取锁：A");
            synchronized (A) {
                try {
                    System.out.println("线程t1获取到了锁：A");
                    Thread.currentThread().sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("线程t1等待获取锁：B");
                synchronized (B) {
                    System.out.println("线程t1获取到了锁：B");
                }
                System.out.println("线程t1释放了锁：B");
            }
            System.out.println("线程t1释放了锁：A");
        });
        Thread t2 = new Thread(() -> {
            System.out.println("线程t2等待获取锁：B");
            synchronized (B) {
                try {
                    System.out.println("线程t2获取到了锁：B");
                    Thread.currentThread().sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("线程t2等待获取锁：A");
                synchronized (A) {
                    System.out.println("线程t2获取到了锁：A");
                }
                System.out.println("线程t2释放了锁：A");
            }
            System.out.println("线程t2释放了锁：B");
        });
        t1.start();
        t2.start();
    }

}
